package cst.a931xmc.yuejian20.Activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import cst.a931xmc.yuejian20.Fragment.ClubFragment;
import cst.a931xmc.yuejian20.Fragment.Home.HomeFragment;
import cst.a931xmc.yuejian20.Fragment.Me.MeFragment;
import cst.a931xmc.yuejian20.Fragment.ShowFragment;
import cst.a931xmc.yuejian20.Fragment.Train.TrainFragment;
import cst.a931xmc.yuejian20.R;


public class MainActivity extends FragmentActivity implements View.OnClickListener{
    private LinearLayout layout_train;
    private LinearLayout layout_club;
    private LinearLayout layout_home;
    private LinearLayout layout_show;
    private LinearLayout layout_me;
    private ImageButton ib_train;
    private ImageButton ib_club;
    private ImageButton ib_home;
    private ImageButton ib_show;
    private ImageButton ib_me;
    private TextView tv_train;
    private TextView tv_club;
    private TextView tv_home;
    private TextView tv_show;
    private TextView tv_me;
    private Fragment fragmentTrain;
    private Fragment fragmentClub;
    private Fragment fragmentHome;
    private Fragment fragmentShow;
    private Fragment fragmentMe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        resetImgs();
        setSelect(2);
    }

    public void init(){
        layout_train = findViewById(R.id.layout_train);
        layout_club = findViewById(R.id.layout_club);
        layout_home = findViewById(R.id.layout_home);
        layout_show = findViewById(R.id.layout_show);
        layout_me = findViewById(R.id.layout_me);
        ib_train = findViewById(R.id.ib_train);
        ib_club = findViewById(R.id.ib_club);
        ib_home = findViewById(R.id.ib_home);
        ib_show = findViewById(R.id.ib_show);
        ib_me = findViewById(R.id.ib_me);
        tv_train = findViewById(R.id.tv_train);
        tv_club = findViewById(R.id.tv_club);
        tv_home = findViewById(R.id.tv_home);
        tv_show = findViewById(R.id.tv_show);
        tv_me = findViewById(R.id.tv_me);
        layout_train.setOnClickListener(this);
        layout_club.setOnClickListener(this);
        layout_home.setOnClickListener(this);
        layout_show.setOnClickListener(this);
        layout_me.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        resetImgs();
        switch (v.getId()){
            case R.id.layout_train:
                setSelect(0);
                break;
            case R.id.layout_club:
                setSelect(1);
                break;
            case R.id.layout_home:
                setSelect(2);
                break;
            case R.id.layout_show:
                setSelect(3);
                break;
            case R.id.layout_me:
                setSelect(4);
                break;
            default:
                break;
        }
    }

    public void setSelect(int i){
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                if(fragmentTrain == null){
                    fragmentTrain = new TrainFragment();
                    transaction.add(R.id.id_content, fragmentTrain);
                }
                else {
                    transaction.show(fragmentTrain);
                }
                ib_train.setImageResource(R.drawable.trained);
                tv_train.setTextColor(getResources().getColor(R.color.colorWordRed));
                break;
            case 1:
                if(fragmentClub == null){
                    fragmentClub = new ClubFragment();
                    transaction.add(R.id.id_content, fragmentClub);
                }
                else {
                    transaction.show(fragmentClub);
                }
                ib_club.setImageResource(R.drawable.clubed);
                tv_club.setTextColor(getResources().getColor(R.color.colorWordRed));
                break;
            case 2:
                if(fragmentHome == null){
                    fragmentHome = new HomeFragment();
                    transaction.add(R.id.id_content, fragmentHome);
                }
                else {
                    transaction.show(fragmentHome);
                }
                ib_home.setImageResource(R.drawable.homed);
                tv_home.setTextColor(getResources().getColor(R.color.colorWordRed));
                break;
            case 3:
                if(fragmentShow == null){
                    fragmentShow = new ShowFragment();
                    transaction.add(R.id.id_content, fragmentShow);
                }
                else {
                    transaction.show(fragmentShow);
                }
                ib_show.setImageResource(R.drawable.showed);
                tv_show.setTextColor(getResources().getColor(R.color.colorWordRed));
                break;
            case 4:
                if(fragmentMe == null){
                    fragmentMe = new MeFragment();
                    transaction.add(R.id.id_content, fragmentMe);
                }
                else {
                    transaction.show(fragmentMe);
                }
                ib_me.setImageResource(R.drawable.meed);
                tv_me.setTextColor(getResources().getColor(R.color.colorWordRed));
                break;
        }
        transaction.commit();
    }

    public void hideFragment(FragmentTransaction transaction){
        if(fragmentTrain != null){
            transaction.hide(fragmentTrain);
        }
        if(fragmentClub != null){
            transaction.hide(fragmentClub);
        }
        if(fragmentHome != null){
            transaction.hide(fragmentHome);
        }
        if(fragmentShow != null){
            transaction.hide(fragmentShow);
        }
        if(fragmentMe != null){
            transaction.hide(fragmentMe);
        }
    }

    public void resetImgs(){
        ib_train.setImageResource(R.drawable.train);
        tv_train.setTextColor(getResources().getColor(R.color.colorWordGray));
        ib_club.setImageResource(R.drawable.club);
        tv_club.setTextColor(getResources().getColor(R.color.colorWordGray));
        ib_home.setImageResource(R.drawable.home);
        tv_home.setTextColor(getResources().getColor(R.color.colorWordGray));
        ib_show.setImageResource(R.drawable.show);
        tv_show.setTextColor(getResources().getColor(R.color.colorWordGray));
        ib_me.setImageResource(R.drawable.me);
        tv_me.setTextColor(getResources().getColor(R.color.colorWordGray));
    }
}
